﻿/*======================================
作者：洞庭夕照
创建：2017.10.11
网站：www.ninesky.cn
      mzwhj.cnblogs.com
支持：bbs.ninesky.cn
代码：git.oschina.net/ninesky/Ninesky
版本：v1.0.0.0
======================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Ninesky.Models;
using Ninesky.IService;
using Ninesky.MVC.ViewModels;


namespace Ninesky.MVC.Areas.Manage.Controllers
{
    [Area("Manage")]
    public class CategoryController : Controller
    {
        private ICategoryService _categoryService;

        public CategoryController(ICategoryService categoryService)
        {
            _categoryService = categoryService;
        }
        public IActionResult Index()
        {
            return View();
        }

        #region 查询Json
        /// <summary>
        /// 查找父栏目列表
        /// </summary>
        /// <returns>父栏目列表的json类型</returns>
        public async Task<IActionResult> GetParentCategoryList()
        {
            var categories = await _categoryService.FindListAsync<int>(0, c => c.Type == CategoryType.General, c => c.CategoryId, true);
            return Json(categories.ToList());
        }

        /// <summary>
        /// 查找所有栏目
        /// </summary>
        /// <returns>栏目列表</returns>
        public async Task<IActionResult> GetCategoryList()
        {
            var categories = await _categoryService.FindAllAsync();
            return Json(categories.ToList());
        }

        #endregion

        #region 添加
        public IActionResult Add()
        {
            return View(new Category());
        }

        [HttpPost]
        public async Task<IActionResult> Add(Category category)
        {

            var opsResult = await _categoryService.AddAsync(category);
            if (opsResult.Succeed)
            {
                NoticeViewModel notice = new NoticeViewModel { Type = NoticeType.Succeed, Tile = "添加成功", Message = "添加栏目【" + category.Name + "】成功。", Links = new Dictionary<string, string>() };
                notice.Links.Add("继续添加", "/Manage/Category/Add");
                notice.Links.Add("修改栏目", "/Manage/Category/Modify/" + category.CategoryId);
                return View("Notice", notice);
            }
            else if (opsResult.Code == 1)
            {
                foreach (var item in (Dictionary<string, string>)opsResult.Data)
                {
                    ModelState.AddModelError(item.Key, item.Value);
                }
            }
            else ModelState.AddModelError("", opsResult.Message);
            return View(category);
        }

        #endregion

        #region 修改

        public async Task<IActionResult> Modify(int id)
        {
            return View(await _categoryService.FindAsync(id));
        }
        [HttpPost]
        public async Task<IActionResult> Modify(Category category)
        {
            if (ModelState.IsValid)
            {
                var opsResult = await _categoryService.UpdateAsync(category);
                if (opsResult.Succeed)
                {
                    var notice = new NoticeViewModel { Type = NoticeType.Succeed, Tile = "修改栏目成功", Message = "您已成功修改栏目【" + category.Name + "】的信息。", Links = new Dictionary<string, string>() };
                    notice.Links.Add("添加栏目", Url.Content("~/Manage/Category/Add"));
                    notice.Links.Add("修改栏目", Url.Content("~/Manage/Category/Modify/" + category.CategoryId));
                    notice.Links.Add("栏目首页", Url.Content("~/Manage/Category/Index"));
                    return View("Notice", notice);
                }
                else if (opsResult.Code == 1)
                {
                    foreach (var item in (Dictionary<string, string>)opsResult.Data)
                    {
                        ModelState.AddModelError(item.Key, item.Value);
                    }
                }
                else ModelState.AddModelError("", opsResult.Message);
            }
            return View(category);
        }

        #endregion

        #region 删除
        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            var opsResult = await _categoryService.RemoveAsync(new Ninesky.Models.Category() { CategoryId = id });
            return new JsonResult(opsResult);
        }
        #endregion
    }
}
